#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
    int n,k;
    cin>>n>>k;
    int ans=k+1;
    if(n%2&&n!=1)ans+=k-1;
    while(n&&k)
    {
        n/=2;
        k--;
        ans++;
        if(n%2&&n!=1)ans+=k-1;
    }
    cout<<ans<<endl;
}
signed main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    int T=1;
    cin>>T;
    while(T--)
    {
       solve();
    }
    return 0;
}